package cn.stalk.wf.event;

import org.apache.commons.lang3.StringUtils;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.DelegateTask;
import org.camunda.bpm.engine.delegate.ExecutionListener;
import org.camunda.bpm.engine.delegate.TaskListener;

import lombok.extern.slf4j.Slf4j;

/**
 * 任务节点自动分配办理人事件
 */
@Slf4j
public class UserTaskAutoAssignmentListener implements TaskListener, ExecutionListener {

    @Override
    public void notify(DelegateTask delegateTask) {
        log.info("StartNodeListener-task-----------------" + delegateTask.getEventName() + ">>" + delegateTask.getId());
        if (TaskListener.EVENTNAME_CREATE.equals(delegateTask.getEventName())) {
            assignTask(delegateTask);
        }
    }

    @Override
    public void notify(DelegateExecution execution) throws Exception {
        log.info("StartNodeListener-execution------------" + execution.getEventName() + ">>" + execution.getId());
    }

    private void assignTask(DelegateTask delegateTask) {
        if (StringUtils.isNotBlank(delegateTask.getAssignee())) {
            return; // 已指派
        }

        // delegateTask.getCandidates().forEach(e -> {
        // log.info("--->{}", e);
        // });
    }

}
